<!-- 列表数据 -->
<template>
  <div class="system-pdtable-list-data-table" v-loading="loading">
    <div class="header clearfix">
      <div class="tb-opt">
        <el-button type="text" @click="deletePdtables">批量删除</el-button>
        <el-button type="text" @click="updateCategory">批量修改分类</el-button>
      </div>
      <div class="toppage">
        <PageTurning :page="page" :changePage="changePage" />
      </div>
    </div>
    <DataList :dataTable="dataTable" :data="data" :customeTitle="customeTitle" :pdTitle="pdTitle" :multipleSelection="multipleSelection"
      :multipleSelectionChange="multipleSelectionChange" :resetData="resetData" :system="system"
      :setDataTable="setDataTable" :page="page" />
    <div class="footer">
      <PageTurning :page="page" :changePage="changePage" />
    </div>
    <div style="clear:both;"></div>
    <UpdateCategory ref="updateCategory" :resetData="resetData"/>
  </div>
</template>

<script>
import PageTurning from '@/components/common/PageTurning.vue'
import DataList from '@/components/main/user/system/pdtable/DataList.vue'
import UpdateCategory from '@/components/main/user/system/category/common/UpdateCategory.vue'
import * as functions from '@/core/functions.js'
import * as WindowType from '@/config/windowtype.js'
import PubSub from 'pubsub-js'
export default {
  created () {
    let query = this.$route.query
    if (!query || !query.sid) {
      this.$message.error('未能获取到正确的数据')
      return
    }
    this.systemId = parseInt(query.sid)
    PubSub.subscribe(WindowType.REFRESH_PDTABLE_TABLELIST, (event, data) => {
      this.resetData()
    })
  },
  props: {
    search: {
      type: Object
    },
    dataTotal: {
      type: Function
    },
    system: {
      type: Object
    }
  },
  mounted () {
    // 判断是否从编辑页返回获取编码
    let systbPageCurInfo = JSON.parse(sessionStorage.getItem('systbPageCur' + this.systemId))
    if (systbPageCurInfo && systbPageCurInfo.fromItem) {
      this.page.page = systbPageCurInfo.pageCur
    }
    // 请求获取数据
    this.resetData()
  },
  data () {
    return {
      // 加载中
      loading: false,
      // 系统id
      systemId: 0,
      // 非回收站内的数据总数
      total: 0,
      page: {
        // 当前页
        page: 1,
        // 一页大小
        size: 12,
        // 总数
        total: 0
      },
      // 请求时获取的数据
      data: {},
      // 表格显示的数据
      dataTable: [],
      // 批量删除选中部分
      multipleSelection: [],
      // 自定义属性标题
      customeTitle: [],
      // 产品固定标题
      pdTitle: {}
    }
  },
  methods: {
    multipleSelectionChange (val) {
      this.multipleSelection = val
    },
    resetData (page) {
      if (typeof page !== 'undefined') {
        this.page = Object.assign({}, this.page, page)
      }
      // 获取数据
      let param = Object.assign({ systemId: this.systemId }, this.search, this.page)
      // if (!param.createAt) {
      //   param.createAt = []
      // }
      this.loading = true
      this.$jpost('/system/pdtable/data-infos', param).then(response => {
        if (response) {
          if (response.total) {
            this.page.total = response.total
          } else {
            this.page.total = 0
          }
          this.dataTotal(this.page.total)
          if (response.title) {
            this.customeTitle = initCustometitle(response.title).result
            this.pdTitle = initCustometitle(response.title).pdTitle
          } else {
            this.customeTitle = []
            this.pdTitle = {}
          }
          if (response.rows && response.rows.length > 0) {
            this.data = response
            this.dataTable = initData(response)
          } else {
            this.dataTable = []
            this.data = {}
          }
        }
        this.loading = false
      })
    },
    // 改变页面
    changePage (page) {
      this.page = JSON.parse(JSON.stringify(page))
      this.resetData()
    },
    // 批量删除
    deletePdtables () {
      if (this.multipleSelection.length < 1) {
        this.$message.error('请选择要删除的数据！')
      } else {
        let ids = []
        for (let i in this.multipleSelection) {
          ids.push(this.multipleSelection[i].id)
        }
        let param = {
          ids: ids,
          systemId: this.systemId
        }
        this.$confirm('确认要删除选中的' + ids.length + '个产品项？', '提示', {
          type: 'warning'
        })
          .then(() => {
            this.$jpost('/system/pdtable/data-deletes', param).then(response => {
              if (response && response.length > 0) {
                this.resetData()
              }
            })
          })
          .catch(() => {
          })
      }
    },
    // 重置dataTable
    setDataTable (dataTable) {
      this.dataTable = dataTable
    },
    // 批量更改分类
    updateCategory () {
      if (this.multipleSelection.length < 1) {
        this.$message.error('请选择要更改分类的数据！')
      } else {
        let param = []
        for (let i in this.multipleSelection) {
          param.push(this.multipleSelection[i].id)
        }
        this.$refs.updateCategory.show(this.system, param)
      }
    }
  },
  beforeDestroy () {
    // 存储系统页面页码，如果从编辑页返回列表页fromItem改为true
    let systbPageCur = JSON.parse(sessionStorage.getItem('systbPageCur' + this.systemId))
    if (systbPageCur) {
      systbPageCur.fromItem = false
      sessionStorage.setItem('systbPageCur' + this.systemId, JSON.stringify(systbPageCur))
    }
  },
  components: {
    PageTurning,
    DataList,
    UpdateCategory
  }
}
function initData (data) {
  let result = []
  if (!data.rows || data.rows.length < 1) {
    return []
  }
  data.rows.forEach(item => {
    let arr = initInfo(item, data)
    if (arr.id > 0) {
      result.push(arr)
    }
  })
  return result
}
function initInfo (rowinfo, dataAll) {
  let file = rowinfo.file && rowinfo.file.hrepertory && rowinfo.file.srepertoryQuote ? {
    id: rowinfo.file.hrepertory.id,
    originalFilename: rowinfo.file.hrepertory.originalFilename,
    suffix: rowinfo.file.hrepertory.suffix
  } : {}
  let fileRepertoryId = rowinfo && rowinfo.file && rowinfo.file.hrepertory ? rowinfo.file.hrepertory.id : null

  let imageList = []
  let imageRepertoryId = null
  let imageRepertoryConfig = {}
  if (rowinfo && rowinfo.image && rowinfo.image.hrepertory && rowinfo.image.srepertoryQuote) {
    let item = rowinfo.image.hrepertory
    let quote = rowinfo.image.srepertoryQuote
    let config
    if (quote.config) {
      config = JSON.parse(quote.config)
    }
    let title = config && config.title ? config.title : item.originalFilename
    let alt = config && config.alt ? config.alt : item.originalFilename

    let repertoryImageSrc = { hostId: dataAll.system.hostId, repertoryId: item.id, suffix: item.suffix, type: item.type, link: item.link }
    imageList.push({
      id: item.id,
      src: functions.getRepertoryImageSrc(repertoryImageSrc),
      title: title,
      alt: alt
    })
    imageRepertoryId = item.id
    imageRepertoryConfig = {
      title: title,
      alt: alt
    }
  }

  let titleItem = []
  if (rowinfo && rowinfo.listItem && rowinfo.listItem.length > 0 && dataAll.title && dataAll.title.length > 0) {
    dataAll.title.forEach(el => {
      if (el.type === 1 || el.type === 5) {
        let ite = {
          pdtableTitleId: el.id,
          title: el.title,
          value: ''
        }
        rowinfo.listItem.forEach(ele => {
          if (el.id === ele.pdtableTitleId) {
            ite.value = ele.value
          }
        })
        titleItem.push(ite)
      }
    })
  }

  let listCageData = []
  if (rowinfo.listCategoryQuote && rowinfo.listCategoryQuote.length > 0) {
    rowinfo.listCategoryQuote.forEach(item => {
      listCageData.push(item.scategories)
    })
  }

  return {
    id: rowinfo.data.id,
    systemId: dataAll.system.id,
    status: rowinfo.data.status,
    titleItem: titleItem,
    file: file,
    fileRepertoryConfig: '',
    fileRepertoryId: fileRepertoryId,
    imageList: imageList,
    imageRepertoryId: imageRepertoryId,
    imageRepertoryConfig: imageRepertoryConfig,
    releasedAt: rowinfo.data.releasedAt,
    // 分类
    listCageData: listCageData
  }
}
function initCustometitle (data) {
  let result = []
  let pdTitle = {}
  if (!data || data.length < 1) {
    return []
  }
  let num = 0
  data.forEach((element, index) => {
    if ((element.type === 1) && index - num < 20) {
      result.push({
        pdtableTitleId: element.id,
        title: element.title
      })
    } else if (element.type === 5) {
      pdTitle = {
        pdtableTitleId: element.id,
        title: element.title
      }
    } else if (element.type !== 1) {
      num++
    }
  })
  return {result, pdTitle}
}
</script>
<style scoped lang="scss">
.header{
  padding: 10px;
  background: #fafafa;
  margin-bottom: 5px;
  .tb-opt{
    float: left;
  }
  .toppage{
    float: right;
    margin-top: 5px;
  }
}
.system-pdtable-list-data-table .footer {
  float: right;
  margin: 12px 0 24px;
}
</style>
